package com.vhall.component.controller.logger;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vhall.component.controller.account.facade.logger.OperateLogFacade;
import com.vhall.component.entity.logger.OperateLogEntity;
import com.vhall.component.entity.logger.vo.OperateLogPageReqVO;
import com.vhall.component.entity.logger.vo.OperateLogPageRespVO;
import com.vhall.component.entity.logger.vo.OperateLogRespVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.utils.CollectionUtils;
import com.vhall.component.plugin.common.annotation.UrlDisable;
import com.vhall.component.service.account.AccountService;
import com.vhall.component.service.logger.OperateLogService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@UrlDisable
@VhallComponent
public class OperateLogController implements OperateLogFacade {

    @Resource
    private OperateLogService operateLogService;

    @Resource
    private AccountService accountService;


    @Override
    public OperateLogPageRespVO pageOperateLog(@Valid OperateLogPageReqVO reqVO) {
        Integer pagesize = reqVO.getPagesize();
        if (pagesize>=500) {
            pagesize = 100;
        }

        Page<OperateLogEntity> pageResult = operateLogService.getOperateLogPage(reqVO);

        List<OperateLogRespVO> list = new ArrayList<>();
        // 获得拼接需要的数据
        List<OperateLogEntity> records = pageResult.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            records.forEach(operateLog -> {
                OperateLogRespVO respVO = new OperateLogRespVO();
                BeanUtils.copyProperties(operateLog, respVO);
                list.add(respVO);
            });
        }

        OperateLogPageRespVO resp = new OperateLogPageRespVO();
        resp.setCurrentPage(reqVO.getPage());
        resp.setPerPage(pagesize);
        resp.setTotal(pageResult.getTotal());
        resp.setData(list);
        return resp;
    }


}
